//
//  FWAttentionController.m
//  BirdLoveSheep
//
//  Created by wwj on 16/3/9.
//  Copyright © 2016年 wwj. All rights reserved.
//

#import "FWAttentionController.h"
#import "FWAttentService.h"
#import "FWAttentTypeCell.h"
#import "FWAttentUserController.h"
#import <SVProgressHUD.h>

@interface FWAttentionController ()<UITableViewDataSource, UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIView *contentView;
@property (strong, nonatomic) NSArray *typeArray;
@property (strong, nonatomic) FWAttentUserController *attentUserVC;
@end

@implementation FWAttentionController
static NSString * const ID = @"FWAttentTypeCell";

- (NSArray *)typeArray{
    if (_typeArray == nil) {
        _typeArray = [NSArray array];
    }
    return _typeArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    [self initUserTableView];
    
    [self.tableView registerNib:[UINib nibWithNibName:ID bundle:nil] forCellReuseIdentifier:ID];
    [self getTypeData];
}

- (void)initUserTableView{
    self.navigationItem.title = @"推荐关注";
    self.view.backgroundColor = kBgColor;
    
    FWAttentUserController *attentUserVC = [[FWAttentUserController alloc] init];
    attentUserVC.view.frame = _contentView.bounds;
    [_contentView addSubview:attentUserVC.tableView];
    [self addChildViewController:attentUserVC];
    _attentUserVC = attentUserVC;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.typeArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    FWAttentTypeCell *cell = [tableView dequeueReusableCellWithIdentifier:ID forIndexPath:indexPath];
    cell.modle = self.typeArray[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self selectRow:indexPath.row];
}

- (void)selectRow:(NSInteger)row{
    _attentUserVC.model = self.typeArray[row];
}

- (void)getTypeData{
    FWTypeRequest *request = [[FWTypeRequest alloc] init];
    request.a = @"category";
    request.c = @"subscribe";
    [FWAttentService getTypeDataWith:request success:^(FWTypeResponse *response) {
        self.typeArray = response.list;
        [self.tableView reloadData];
        [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionTop];
        [self selectRow:0];
        
    } failure:nil];
}

- (void)dealloc{
    [SVProgressHUD dismiss];
    [FWHttpUtil cancelManager];
}

@end
